package com.goalias.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.goalias.domain.dto.DownloadDto;
import com.goalias.exception.ExceptionEnum;
import com.goalias.resp.Result;
import com.goalias.service.IFileService;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/ability/file")
public class FileController {

    @Resource
    private IFileService fileService;
    @PostMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file,@RequestParam Long categoryId){
        //categoryId为 -1->其他上传  0->用户上传  其他->内容上传
        if (ObjectUtil.isEmpty(file) || categoryId == null){
            return Result.error(ExceptionEnum.PARAM_NOT_VALID);
        }
        return fileService.upload(file,categoryId);
    }
    @GetMapping("/getUrl")
    public Result getUrl(String bucket,String fileName){
        if (!StringUtils.hasText(fileName)){
            return Result.error(ExceptionEnum.PARAM_NOT_VALID);
        }
        return fileService.getUrl(bucket, fileName);
    }
    @PostMapping("/deleteObject")
    public Result deleteObject(@RequestBody Map<String, List<String>> requestBody){
        List<String> fileNames = requestBody.get("fileNames");
        if (CollUtil.isEmpty(fileNames)){
            return Result.error(ExceptionEnum.PARAM_NOT_VALID);
        }
        return fileService.delete(fileNames);
    }
}
